/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionmagasins_aspectj_junit_patterns.forms;

import gestionmagasins_aspectj_junit_patterns.model.CatalogueClients;
import gestionmagasins_aspectj_junit_patterns.model.CatalogueVente;
import gestionmagasins_aspectj_junit_patterns.model.Client;
import gestionmagasins_aspectj_junit_patterns.model.Usager;
import gestionmagasins_aspectj_junit_patterns.model.Vente;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class ManageClientsJFrame extends CustomJFrame {

    /**
     * Creates new form ManageClientsJFrame
     */
    
    CatalogueClients clients;
    Client clientselected;
    
    ManageClientsJFrame(MainMenuJFrame mainMenuJFrame, Usager usager) 
    {
        super(mainMenuJFrame, usager);
        
        initComponents();
        this.initKeyListeners();
        
        //Obtiens la référence du catalogue
        clients = CatalogueClients.getInstance();
        
        cmdActivate.setVisible(false);
        
        RefreshLstClient(clients.getClients());
        GestionIdentification(false);
    }

    /*Rafraichis les listes des client*/
    private void RefreshLstClient(ArrayList<Client> lst)
    {
        DefaultListModel model = new DefaultListModel();
        DefaultListModel desactivatemodel = new DefaultListModel();
        for(Client c: lst)
        {
            if (c.isActivate()) {
                model.addElement(c);
            }else
            {
                desactivatemodel.addElement(c);
            }
            
        }
        lstClients.setModel(model);
        lstDesactivateClients.setModel(desactivatemodel);
        
    }
    
    /*Rafraichis la liste avec les clients trouver par la recherche*/
    private void RechercheClient(String nom)
    {                
        RefreshLstClient(clients.rechercheClient(nom));
    }
    
    /*Activer ou desactiver les boutons Supprimer et Enregistrer*/
    private void GestionBoutonSupprimerEnregister(boolean enableEnregistrer,boolean enableSupprimer)
    {
        cmdEnregistrerClient.setEnabled(enableEnregistrer);
        cmdSupprimerClient.setEnabled(enableSupprimer);
    }
    
    /*Efface les éléments des controles d'iddentifdication et désactive les bontons de gestion*/
    private void ClearIdentification()
    {
        txtNomClient.setText("");
        txtPrenomClient.setText("");
        txtAdresseClient.setText("");
        txtTelephoneClient.setText("");
        dtpDateNaissanceClient.setDate(null);
        
        optSexeMasculinClient.setSelected(true);
        
        clientselected = null;
        GestionBoutonSupprimerEnregister(false,false);
    }
    
    /*Valide si chaque controles d'identification sont adéquat pour faire l'enregistrement du client*/
    private boolean Validation() 
    {
        boolean validate = true;
        
        if (txtAdresseClient.getText().equals("")) 
        {
            txtAdresseClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
            validate = false;
        }
        else
        {
            txtAdresseClient.setBorder(null);
        }
        
        if (txtNomClient.getText().equals("")) 
        {
            txtNomClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
            validate = false;
        }
        else
        {
            txtNomClient.setBorder(null);
        }
        
        if (txtPrenomClient.getText().equals("")) 
        {
            txtPrenomClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
            validate = false;
        }
        else
        {
            txtPrenomClient.setBorder(null);
        }
        
        if (txtTelephoneClient.getText().equals(""))
        {
            txtTelephoneClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
            validate = false;
        }
        else
        {
            if (!isPhoneNumberValid(txtTelephoneClient.getText())) 
            {
               txtTelephoneClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
               validate = false;
            }
            else
            {
                 txtTelephoneClient.setBorder(null);
            }
        }

        if (dtpDateNaissanceClient.getDate() == null) 
        {
            dtpDateNaissanceClient.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.red));
            validate = false;
        }
        else
        {
            dtpDateNaissanceClient.setBorder(null);
        }
        
        return validate;
                
    }
    
    /*Réinitialise les controle sans les bordure rouge de validation*/
    private void ClearValidation()
    {
            txtAdresseClient.setBorder(null);
            txtNomClient.setBorder(null);
            txtPrenomClient.setBorder(null);
            txtTelephoneClient.setBorder(null);
            dtpDateNaissanceClient.setBorder(null);          
    }
    
    /*Active/Désactive la zone d'identification*/
    private void GestionIdentification(boolean enable) 
    {
        Component[] com = pnlIdentification.getComponents();  
        //Inside you action event where you want to disable everything  
        //Do the following  
        for (int a = 0; a < com.length; a++) 
        {  
             com[a].setEnabled(enable); 
        } 
    }
    
    /*Vérifie si la chaine de caractère reçu est valide*/
    private boolean isPhoneNumberValid(String phoneNumber)
    {
        boolean validate = true;
        
        if (!phoneNumber.matches("^\\d{10}$")) 
        {
           validate = false;
        }
        
        return validate;
    }
    
    /*Vérifie si le client a aucune vente*/
    private boolean ClientNoVente(Client deletedclient)
    {
        boolean validate = true;
        
        for(Vente vente : CatalogueVente.getCatalogueVente().getVentes())
        {
            if (vente.getClient().equals(deletedclient))
            {
                validate = false;
            }
        }
        
        return validate;
    }
    
    
        private boolean ClientisModify() 
        {
            boolean isModify = false;
        
            if (clientselected != null && clientselected.getNom() != null)
            {               
                if (!txtAdresseClient.getText().equals(clientselected.getAdresse())) 
                {
                    isModify = true;
                }

                if (!txtNomClient.getText().equals(clientselected.getNom())) 
                {
                    isModify = true;
                }

                if (!txtPrenomClient.getText().equals(clientselected.getPrenom())) 
                {
                    isModify = true;
                }

                if (!txtTelephoneClient.getText().equals(clientselected.getTelephone()))
                {
                    isModify = true;
                }

                if (!(dtpDateNaissanceClient.getDate().toString().equals(clientselected.getDateNaissance().toString()))) 
                {
                    isModify = true;
                }
            }
            else
            {
                if (clientselected != null)
                {
                    isModify = true;
                }
            }

            return isModify;
        }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btngrpSexe = new javax.swing.ButtonGroup();
        jScrollBar1 = new javax.swing.JScrollBar();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtRechercheClient = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstClients = new javax.swing.JList();
        pnlIdentification = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNomClient = new javax.swing.JTextField();
        txtPrenomClient = new javax.swing.JTextField();
        txtAdresseClient = new javax.swing.JTextField();
        txtTelephoneClient = new javax.swing.JTextField();
        optSexeMasculinClient = new javax.swing.JRadioButton();
        optSexeFemininClient = new javax.swing.JRadioButton();
        dtpDateNaissanceClient = new org.jdesktop.swingx.JXDatePicker();
        cmdNouveauClient = new javax.swing.JButton();
        cmdAnnulerClient = new javax.swing.JButton();
        cmdSupprimerClient = new javax.swing.JButton();
        cmdEnregistrerClient = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstDesactivateClients = new javax.swing.JList();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        cmdActivate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Super Logiciel inc.");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setText("Gestion des clients");

        jLabel2.setText("Rechercher :");

        txtRechercheClient.setName("txtRechercheClient"); // NOI18N
        txtRechercheClient.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtRechercheClientKeyPressed(evt);
            }
        });

        lstClients.setName(""); // NOI18N
        lstClients.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstClientsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstClients);

        pnlIdentification.setBorder(javax.swing.BorderFactory.createTitledBorder("Identification"));

        jLabel3.setText("Nom :");
        jLabel3.setName(""); // NOI18N

        jLabel4.setText("Prénom :");

        jLabel5.setText("Adresse :");

        jLabel6.setText("Téléphone :");

        jLabel7.setText("Date de naissance :");

        jLabel8.setText("Sexe :");

        txtNomClient.setName("txtNomClient"); // NOI18N

        txtPrenomClient.setName("txtPrenomClient"); // NOI18N

        txtAdresseClient.setName("txtAddresseClient"); // NOI18N

        txtTelephoneClient.setName("txtTelephoneClient"); // NOI18N

        btngrpSexe.add(optSexeMasculinClient);
        optSexeMasculinClient.setSelected(true);
        optSexeMasculinClient.setText("Masculin");
        optSexeMasculinClient.setName("optSexeMasculinClient"); // NOI18N

        btngrpSexe.add(optSexeFemininClient);
        optSexeFemininClient.setText("Féminin");
        optSexeFemininClient.setName("optSexeClient"); // NOI18N

        dtpDateNaissanceClient.setName("dtpDateNaissanceClient"); // NOI18N

        javax.swing.GroupLayout pnlIdentificationLayout = new javax.swing.GroupLayout(pnlIdentification);
        pnlIdentification.setLayout(pnlIdentificationLayout);
        pnlIdentificationLayout.setHorizontalGroup(
            pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlIdentificationLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtAdresseClient)
                    .addComponent(txtTelephoneClient)
                    .addGroup(pnlIdentificationLayout.createSequentialGroup()
                        .addComponent(optSexeMasculinClient)
                        .addGap(18, 18, 18)
                        .addComponent(optSexeFemininClient))
                    .addComponent(dtpDateNaissanceClient, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNomClient)
                    .addComponent(txtPrenomClient))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        pnlIdentificationLayout.setVerticalGroup(
            pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlIdentificationLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPrenomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtAdresseClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTelephoneClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(dtpDateNaissanceClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(pnlIdentificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(optSexeMasculinClient)
                    .addComponent(optSexeFemininClient))
                .addContainerGap(107, Short.MAX_VALUE))
        );

        cmdNouveauClient.setText("Nouveau");
        cmdNouveauClient.setName("cmdNouveau"); // NOI18N
        cmdNouveauClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNouveauClientActionPerformed(evt);
            }
        });

        cmdAnnulerClient.setText("Annuler");
        cmdAnnulerClient.setName("cmdAnnuler"); // NOI18N
        cmdAnnulerClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAnnulerClientActionPerformed(evt);
            }
        });

        cmdSupprimerClient.setText("Supprimer");
        cmdSupprimerClient.setEnabled(false);
        cmdSupprimerClient.setName("cmdSupprimer"); // NOI18N
        cmdSupprimerClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSupprimerClientActionPerformed(evt);
            }
        });

        cmdEnregistrerClient.setText("Enregistrer");
        cmdEnregistrerClient.setEnabled(false);
        cmdEnregistrerClient.setName("cmdEnregistrer"); // NOI18N
        cmdEnregistrerClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEnregistrerClientActionPerformed(evt);
            }
        });

        lstDesactivateClients.setName(""); // NOI18N
        lstDesactivateClients.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstDesactivateClientsValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lstDesactivateClients);

        jLabel9.setText("Désactivé");

        jLabel10.setText("Activer");

        cmdActivate.setText("Activer");
        cmdActivate.setName("cmdNouveau"); // NOI18N
        cmdActivate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdActivateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtRechercheClient))
                                .addComponent(jScrollPane1)
                                .addComponent(jScrollPane2))
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addGap(26, 26, 26)
                        .addComponent(pnlIdentification, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 1, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cmdActivate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdNouveauClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdAnnulerClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdEnregistrerClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSupprimerClient)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtRechercheClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlIdentification, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNouveauClient)
                    .addComponent(cmdAnnulerClient)
                    .addComponent(cmdSupprimerClient)
                    .addComponent(cmdEnregistrerClient)
                    .addComponent(cmdActivate))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /*Recherche le client avec le nom envoyer*/
    private void txtRechercheClientKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRechercheClientKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            if (txtRechercheClient.getText().equals("")) 
            {
                RefreshLstClient(clients.getClients());
            }
            else
            {
                RechercheClient(txtRechercheClient.getText());
            }
        }
    }//GEN-LAST:event_txtRechercheClientKeyPressed

    /*Affiche les donnée du client sélectionné*/
    private void lstClientsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstClientsValueChanged
        if (!evt.getValueIsAdjusting())
        {
            if (lstClients.getSelectedIndex() != -1)
            {
                lstDesactivateClients.setSelectedIndex(-1);
                cmdActivate.setVisible(false);
                
                GestionBoutonSupprimerEnregister(true,true);
                GestionIdentification(true);
                
                clientselected = (Client)lstClients.getSelectedValue();
                
                txtNomClient.setText(clientselected.getNom());
                txtPrenomClient.setText(clientselected.getPrenom());
                txtAdresseClient.setText(clientselected.getAdresse());
                txtTelephoneClient.setText(clientselected.getTelephone());
                dtpDateNaissanceClient.setDate(clientselected.getDateNaissance());
                
                if (clientselected.getSexe().equals("Masculin"))
                {
                    optSexeMasculinClient.setSelected(true);
                }
                else
                {
                    optSexeFemininClient.setSelected(true);
                }
            }
        }
    }//GEN-LAST:event_lstClientsValueChanged

    /*Vide la zone d'identificatrion pour recevoir les données du nouveau client*/
    private void cmdNouveauClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNouveauClientActionPerformed
        GestionIdentification(true);
        ClearIdentification();
        lstClients.setSelectedIndex(-1);
        clientselected = new Client();
        GestionBoutonSupprimerEnregister(true,false);
    }//GEN-LAST:event_cmdNouveauClientActionPerformed

    /*Supprime le client ou le désactive*/
    private void cmdSupprimerClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSupprimerClientActionPerformed
        if (ClientNoVente(clientselected)) {
            if (JOptionPane.showConfirmDialog(this, "Voulez-vous vraiment supprimer ce client?", "Attention", JOptionPane.YES_NO_OPTION) == 0) 
            {
                clients.supprimerClient(clientselected);
                ClearIdentification();
                RefreshLstClient(clients.getClients());
            }
        }
        else
        {
            if (JOptionPane.showConfirmDialog(this, "Voulez-vous vraiment désactivé ce client?", "Attention", JOptionPane.YES_NO_OPTION) == 0) 
            {
                clientselected.setActivate(false);    
            }
        }
    }//GEN-LAST:event_cmdSupprimerClientActionPerformed

    /*Enregistrer les modification ou ajoute un nouveau client si la validation est respecté*/
    private void cmdEnregistrerClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEnregistrerClientActionPerformed
        
        if (Validation()) 
        {
            Client client;

            client = clientselected;
            
            client.setNom(txtNomClient.getText());
            client.setPrenom(txtPrenomClient.getText());
            client.setAdresse(txtAdresseClient.getText());
            client.setDateNaissance(dtpDateNaissanceClient.getDate());
            client.setTelephone(txtTelephoneClient.getText());

            if (optSexeMasculinClient.isSelected())
            {
                client.setSexe("Masculin");
            }
            else
            {
                client.setSexe("Féminin");
            }

            if (lstClients.getSelectedIndex() == -1) 
            {
                clients.addClient(client);
            }

            RefreshLstClient(clients.getClients());
            
            ClearIdentification();
            ClearValidation();
            GestionBoutonSupprimerEnregister(false, false);
            GestionIdentification(false);
        }       
    }//GEN-LAST:event_cmdEnregistrerClientActionPerformed

    /*Annule les action entrepris*/
    private void cmdAnnulerClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAnnulerClientActionPerformed
        if (ClientisModify()) 
        {
            if (JOptionPane.showConfirmDialog(this, "Voulez-vous vraiment annuler les changements effectués?", "Attention", JOptionPane.YES_NO_OPTION) == 0) 
            {
                ClearIdentification();
                cmdActivate.setVisible(false);

                lstDesactivateClients.setSelectedIndex(-1);
                GestionIdentification(false);
            }    
        }
    }//GEN-LAST:event_cmdAnnulerClientActionPerformed

    /*Affiche les données d'un client désactivé sélectionné et affiche le bouton activer*/
    private void lstDesactivateClientsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstDesactivateClientsValueChanged
        if (!evt.getValueIsAdjusting())
        {
            if (lstDesactivateClients.getSelectedIndex() != -1)
            {                
                lstClients.setSelectedIndex(-1);                
                
                clientselected = (Client)lstDesactivateClients.getSelectedValue();
                
                txtNomClient.setText(clientselected.getNom());
                txtPrenomClient.setText(clientselected.getPrenom());
                txtAdresseClient.setText(clientselected.getAdresse());
                txtTelephoneClient.setText(clientselected.getTelephone());
                dtpDateNaissanceClient.setDate(clientselected.getDateNaissance());
                
                if (clientselected.getSexe().equals("Masculin"))
                {
                    optSexeMasculinClient.setSelected(true);
                }
                else
                {
                    optSexeFemininClient.setSelected(true);
                }
                clientselected = (Client)lstDesactivateClients.getSelectedValue();
                
                RefreshLstClient(clients.getClients());
                cmdActivate.setVisible(true);
                GestionIdentification(false);
            }
            else
            {
                cmdActivate.setVisible(false);
            }
        }
    }//GEN-LAST:event_lstDesactivateClientsValueChanged

    /*Active le client désactivé*/
    private void cmdActivateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdActivateActionPerformed
               
        clientselected.setActivate(true);
        lstDesactivateClients.setSelectedIndex(-1);
        ClearIdentification();
        RefreshLstClient(clients.getClients());
        cmdActivate.setVisible(false);
        
    }//GEN-LAST:event_cmdActivateActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btngrpSexe;
    private javax.swing.JButton cmdActivate;
    private javax.swing.JButton cmdAnnulerClient;
    private javax.swing.JButton cmdEnregistrerClient;
    private javax.swing.JButton cmdNouveauClient;
    private javax.swing.JButton cmdSupprimerClient;
    private org.jdesktop.swingx.JXDatePicker dtpDateNaissanceClient;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstClients;
    private javax.swing.JList lstDesactivateClients;
    private javax.swing.JRadioButton optSexeFemininClient;
    private javax.swing.JRadioButton optSexeMasculinClient;
    private javax.swing.JPanel pnlIdentification;
    private javax.swing.JTextField txtAdresseClient;
    private javax.swing.JTextField txtNomClient;
    private javax.swing.JTextField txtPrenomClient;
    private javax.swing.JTextField txtRechercheClient;
    private javax.swing.JTextField txtTelephoneClient;
    // End of variables declaration//GEN-END:variables



}
